package com.xungou.good.controller;

import com.xungou.good.pojo.Good;
import com.xungou.good.service.IGoodService;
import com.xungou.goodtypeproperty.service.IGoodTypePropertyService;
import com.xungou.resource.service.IResourceService;
import com.xungou.shop.pojo.Shop;
import com.xungou.shop.service.IShopService;
import com.xungou.utils.FileUtil;
import com.xungou.utils.UserUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/good")
public class GoodController {
    @Resource
    private IGoodService goodService;
    @Resource
    private IShopService shopService;
    @Resource
    private IResourceService iResourceService;
    @Resource
    private IGoodTypePropertyService goodTypePropertyService;

    @RequestMapping("/addGoodPage")
    public ModelAndView addGoodPage() {
        ModelAndView modelAndView = new ModelAndView();
        int userId = UserUtil.getCurrentUser().getId();
        Shop shop = shopService.getShopByUserId(userId);
        if (shop != null && shop.getStatus().equals(Shop.STATUS_CHECK_SUCCESS)) {
            modelAndView.setViewName("home/goodManage/addGood");
        } else {
            modelAndView.addObject("shop", shop);
            modelAndView.setViewName("home/shopManage/authentication");
        }
        return modelAndView;
    }

    @ResponseBody
    @RequestMapping(value = "/add")
    public Map<String, Object> add(@RequestParam(value = "file") MultipartFile file, Good good, @RequestParam Map<String, String> params) {
        return goodService.add(file, good, params);
    }

    @ResponseBody
    @RequestMapping(value = "/delete")
    public Map<String, Object> delete(Integer id) {
        int flag = goodService.deleteByPrimaryKey(id);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("flag", flag);
        return map;
    }

    @ResponseBody
    @RequestMapping(value = "/update")
    public Map<String, Object> update(Good good) {
        int flag = goodService.updateByPrimaryKeySelective(good);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("flag", flag);
        return map;
    }

    @RequestMapping("/serverurl")
    @ResponseBody
    public Map<String, Object> serverurl(HttpServletRequest request, @RequestParam Map<String, String> params){
        Map<String, Object> result = new HashMap<String, Object>();
        String action = params.get("action");
        if(action.equals("config")){
            result.put("state","SUCCESS");
        }else if(action.equals("upLoadImage")){
            MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;             // 获得文件：         MultipartFile file = multipartRequest.getFileMap();
            MultipartFile file = multipartRequest.getFile("undefined");
            com.xungou.resource.pojo.Resource resource = FileUtil.uploadFile(file, "goodDescribe", iResourceService);
            result.put("url","/resource/download.do?id="+resource.getId());
            result.put("state","SUCCESS");
        }else{
            result.put("state","FAIL");
        }
        return result;
    }

    @RequestMapping("/manageGoodPage")
    public ModelAndView manageGoodPage() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("home/goodManage/manageGood");
        return modelAndView;
    }

    @RequestMapping("/list")
    @ResponseBody
    public List<Good> list(@RequestParam Map<String, String> params){
        List<Good> result = goodService.list(params);
        return result;
    }

    @RequestMapping("/searchPage")
    @ResponseBody
    public ModelAndView searchPage(@RequestParam Map<String, String> params){
        ModelAndView modelAndView = new ModelAndView();
        List<Map<String, Object>> goodPropertys = goodTypePropertyService.getTypePropertyTree(params);
        modelAndView.addObject("goodPropertys",goodPropertys);
        modelAndView.setViewName("home/goodSearch/search");
        return modelAndView;
    }

    @RequestMapping("/search")
    @ResponseBody
    public Map<String,Object> search(@RequestParam Map<String, Object> params){
        Map<String,Object> result = goodService.search(params);
        return result;
    }

    @RequestMapping("/detail")
    @ResponseBody
    public ModelAndView detail(@RequestParam Map<String, String> params){
        ModelAndView modelAndView = new ModelAndView();
        Map<String,Object> goodMessage = goodService.getGoodMessage(params);
        modelAndView.addObject("goodMessage",goodMessage);
        modelAndView.setViewName("home/goodSearch/detail");
        return modelAndView;
    }
}